package controller;

import entity.Token;
import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import entity.Result;
import service.TokenService;
import service.UserService;

/**
 * @program: IoT201759225309
 * @description: user控制层
 * @author: Xiaozhuoer
 * @create: 2021-06-01 14:22
 **/

@Controller
@RequestMapping("user")
public class UserController {
    private final UserService userService;
    private final TokenService tokenService;
    public UserController(UserService userService,TokenService tokenService){
        this.userService = userService;
        this.tokenService = tokenService;
    }
    /**
     * 注册
     * @param user
     * @return Result
     */
    @RequestMapping("/logon")
    @ResponseBody
    public Result logonUser(User user){
        System.out.println("获取待注册用户信息: " + user);
        return userService.logonUser(user);
    }
    /**
     * 登录
     * @param user
     * @return Result
     */
    @RequestMapping("/login")
    @ResponseBody
    public Result loginUser(User user){
        Result result = new Result();
        user = (User)userService.loginUser(user).getData();
        if(user != null){
            Token token = tokenService.CreateToken(user);
            //根据type辨别管理、开发、用户
            // token.getType();
            result.setStatus(0);
            result.setMsg("登录成功");
            result.setData(token.getUuid());
        }else{
            result.setStatus(1);
            result.setMsg("登录失败");
            result.setData(null);
        }
        return result;
    }
    /**
     * 通过Email获取用户信息
     * @param user
     * @return Result
     */
    @RequestMapping("/findByEmail")
    @ResponseBody
    public Result findByEmail(User user){
        Result result = new Result();
        result = userService.findByEmail(user);
        result.setStatus(0);
        return result;
    }
    /**
     * 获取所有User信息
     * @return Result
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(){
        Result result = new Result();
        result = userService.findAll();
        result.setStatus(0);
        return result;
    }
    /**
     * 修改User信息
     * @param user
     * @return Result
     */
    @RequestMapping("/modifyUser")
    @ResponseBody
    public Result modifyUser(User user){
        Result result = new Result();
        result = userService.modifyUser(user);
        result.setStatus(0);
        return result;
    }
    /**
     * 删除User
     * @param user
     * @return Result
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Result deleteUser(User user){
        Result result = new Result();
        result = userService.deleteUser(user);
        result.setStatus(0);
        return result;
    }
}
